//
//  ViewController.m
//  05-应用管理
//
//  Created by wangneihan on 15/12/13.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "AppInfo.h"
#import "AppInfoView.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *appInfo;

-(NSArray *) appInfo;

@end

@implementation ViewController

-(NSArray *) appInfo{
    if(_appInfo == nil)
    {
        _appInfo = [AppInfo getAppInfo];
    }
    return _appInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self creatView];
}

// 创建九宫格
-(void)creatView
{
    
    CGFloat viewH = 100;
    CGFloat viewW = 100;
    
    //        子view的横向间距  =  (父view的宽度- 3 * 子view的宽度) / 4
    CGFloat marginX = (self.view.frame.size.width - 3 * viewW) / 4;
    //        子view的纵向间距 =  20
    CGFloat marginY = 20;
    for(int i = 0 ; i < self.appInfo.count ; i ++ )
    {

        //计算frame
        //        当前子view的行号 = 当前遍历到得索引值 / 总列数
        int row = i / 3;
        //        当前子view的列号 = 当前遍历到得索引值 % 总列数
        int column = i % 3;
        //
        //        子view横坐标的公式 =  子view的横向间距  +  列号 * (子view的横向间距+ 子view的宽度)
        CGFloat subViewX = marginX + column * (marginX + viewW);
        //        子view纵坐标的公式 = 20 + 行号 * (子view的纵向间距+ 子view的高度)
        CGFloat subViewY = 30 + row * (marginY + viewH);
        
        AppInfoView *appInfoView = [AppInfoView appInfoView];

        appInfoView.frame = CGRectMake(subViewX, subViewY, viewH, viewW);

        AppInfo *appInfo = self.appInfo[i];
        
        appInfoView.appInfo = appInfo;
        
        [self.view addSubview:appInfoView];
        
//        [view setBackgroundColor:[UIColor redColor]];
//        
//        [self.view addSubview:view];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
